package com.ruoyi.web.controller.ring.app;

import cn.hutool.core.convert.Convert;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.ring.domain.UserPhysiologySet;
import com.ruoyi.ring.domain.dto.PhysiologyReminderDTO;
import com.ruoyi.ring.domain.dto.PhysiologySetDTO;
import com.ruoyi.ring.domain.vo.PhysiologyReminderVO;
import com.ruoyi.ring.domain.vo.PhysiologySetVO;
import com.ruoyi.ring.service.IUserPhysiologySetService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 生理周期设置 - APP
 */
@RestController
@RequestMapping("/ring/app/user_physiology_set")
public class AppUserPhysiologySetController extends BaseController
{
    @Resource
    private IUserPhysiologySetService userPhysiologySetService;


    /**
     * 获取生理周期设置详细信息 - APP
     */
    @GetMapping(value = "/query-set-info")
    public AjaxResult querySetInfo()
    {
        UserPhysiologySet physiologySet = userPhysiologySetService.selectOneUserPhysiologySet(Builder.of(UserPhysiologySet::new).with(UserPhysiologySet::setUserId, SecurityUtils.getUserId()).build());
        PhysiologySetVO setVO = Convert.convert(PhysiologySetVO.class,physiologySet);
        return AjaxResult.success(setVO);
    }

    /**
     * 新增或修改生理周期设置 - APP
     */
    @PostMapping("/set-info")
    public AjaxResult addOrUpdSet(@RequestBody PhysiologySetDTO physiologySetDTO) {
        UserPhysiologySet userPhysiologySet = Convert.convert(UserPhysiologySet.class,physiologySetDTO);
        UserPhysiologySet physiologySet = userPhysiologySetService.selectOneUserPhysiologySet(Builder.of(UserPhysiologySet::new).with(UserPhysiologySet::setUserId, SecurityUtils.getUserId()).build());
        if(physiologySet != null){
            userPhysiologySet.setId(physiologySet.getId());
            return toAjax(userPhysiologySetService.updateUserPhysiologySet(userPhysiologySet));
        }
        userPhysiologySet.setInitialDate(userPhysiologySet.getStartDate());
        return toAjax(userPhysiologySetService.insertUserPhysiologySet(userPhysiologySet));
    }

    /**
     * 获取生理周期通知信息 - APP
     */
    @GetMapping(value = "/query-reminder-info")
    public AjaxResult queryReminderInfo()
    {
        UserPhysiologySet physiologySet = userPhysiologySetService.selectOneUserPhysiologySet(Builder.of(UserPhysiologySet::new).with(UserPhysiologySet::setUserId, SecurityUtils.getUserId()).build());
        PhysiologyReminderVO reminderVO = Convert.convert(PhysiologyReminderVO.class,physiologySet);
        return AjaxResult.success(reminderVO);
    }
    /**
     * 新增或修改生理周期通知信息 - APP
     */
    @PutMapping("/reminder-info")
    public AjaxResult updReminder(@RequestBody PhysiologyReminderDTO reminderDTO)
    {
        UserPhysiologySet userPhysiologySet = Convert.convert(UserPhysiologySet.class,reminderDTO);
        UserPhysiologySet physiologySet = userPhysiologySetService.selectOneUserPhysiologySet(Builder.of(UserPhysiologySet::new).with(UserPhysiologySet::setUserId, SecurityUtils.getUserId()).build());
        userPhysiologySet.setId(physiologySet.getId());
        return toAjax(userPhysiologySetService.updateUserPhysiologySet(userPhysiologySet));
    }
}
